<template>
  <PageWrapper>
    <a-card :bordered="false" class="pt-3 mb-3 proCard">
      <BasicForm
        @register="register"
        @submit="handleSubmit"
        @reset="handleReset"
        @advanced="formAdvanced"
      >
        <template #statusSlot="{ model, field }">
          <a-input v-model="model[field]" placeholder="请输入状态" />
        </template>
      </BasicForm>
    </a-card>
    <a-card :bordered="false" class="proCard">
      <BasicTable
        :columns="columns"
        :request="loadDataTable"
        row-key="id"
        ref="actionRef"
        :actionColumn="actionColumn"
        @update:checked-row-keys="onCheckedRow"
      >
        <template #tableTitle>
          <a-button type="primary" @click="addTable">
            <template #icon>
              <PlusOutlined class="arco-icon" />
            </template>
            新建
          </a-button>
        </template>

        <template #toolbar>
          <a-button type="primary" @click="reloadTable">刷新数据</a-button>
        </template>
      </BasicTable>
    </a-card>
  </PageWrapper>
</template>

<script lang="ts" setup>
  import { reactive, ref, h } from 'vue';
  import { Message } from '@arco-design/web-vue';
  import { BasicTable, TableAction } from '@/components/Table';
  import { PageWrapper } from '@/components/Page';
  import { BasicForm, useForm, FormSchema } from '@/components/Form/index';
  import { getTableList } from '@/api/table/list';
  import { columns } from './columns';
  import { PlusOutlined, DeleteOutlined, FormOutlined, DownOutlined } from '@vicons/antd';

  import { useRouter } from 'vue-router';
  import { renderIcon } from '@/utils';

  const schemas: FormSchema[] = [
    {
      field: 'name',
      labelMessage: '这是一个提示',
      component: 'Input',
      label: '姓名',
      componentProps: {
        placeholder: '请输入姓名',
        onInput: (e: any) => {
          console.log(e);
        },
      },
      rules: [{ required: true, message: '请输入姓名', trigger: ['blur'] }],
    },
    {
      field: 'mobile',
      component: 'InputNumber',
      label: '手机',
      hidden: true,
      componentProps: {
        placeholder: '请输入手机号码',
        hideButton: false,
        onChange: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'type',
      component: 'Select',
      label: '类型',
      componentProps: {
        placeholder: '请选择类型',
        options: [
          {
            label: '舒适性',
            value: 1,
          },
          {
            label: '经济性',
            value: 2,
          },
        ],
        onChange: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'makeDate',
      component: 'DatePicker',
      label: '预约时间',
      //defaultValue: 1183135260000,
      componentProps: {
        onChange: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'status',
      label: '状态',
      //插槽
      slot: 'statusSlot',
    },
    {
      field: 'makeTime',
      component: 'Input',
      label: '停留时间',
      //自定义 高于外面定义的
      // colProps: {
      //   span: 4,
      // },
      componentProps: {
        allowClear: true,
        onChange: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'makeProject',
      component: 'Select',
      label: '预约项目',
      //自定义 高于外面定义的
      // colProps: {
      //   span: 5,
      // },
      componentProps: {
        placeholder: '请选择预约项目',
        options: [
          {
            label: '种牙',
            value: 1,
          },
          {
            label: '补牙',
            value: 2,
          },
          {
            label: '根管',
            value: 2,
          },
        ],
        onChange: (e: any) => {
          console.log(e);
        },
      },
    },
  ];

  const router = useRouter();

  const actionRef = ref();

  const showModal = ref(false);
  const formParams = reactive({
    name: '',
    address: '',
    date: null,
  });

  const params = ref({
    pageSize: 10,
    name: 'xiaoMa',
  });

  const actionColumn = reactive({
    width: 280,
    title: '操作',
    dataIndex: 'action',
    key: 'action',
    fixed: 'right',
    align: 'center',
    render({ record }) {
      return h(TableAction as any, {
        style: 'button',
        actions: [
          {
            label: '删除',
            type: 'primary',
            status: 'danger',
            icon: renderIcon(DeleteOutlined),
            popConfirm: {
              content: '您真的，确定要删除吗？',
              okText: '确定',
              cancelText: '取消',
              onOk: handleDelete.bind(null, record),
              onCancel: handleNegative.bind(null, record),
            },
          },
          {
            label: '编辑',
            type: 'primary',
            icon: renderIcon(FormOutlined),
            popConfirm: {
              content: '您真的，确定要编辑吗？',
              okText: '确定',
              cancelText: '取消',
              onOk: handleEdit.bind(null, record),
              onCancel: handleNegative.bind(null, record),
            },
          },
        ],
        dropDownProps: {
          label: '更多',
          type: 'primary',
          icon: renderIcon(DownOutlined),
          // iconPlacement: 'left',
        },
        dropDownActions: [
          {
            label: '启用',
            size: 'medium',
            key: 'enabled',
            onClick: handleActionSelect.bind(null, record),
            // 根据业务控制是否显示: 非enable状态的不显示启用按钮
            ifShow: () => {
              return true;
            },
          },
          {
            label: '禁用',
            key: 'disabled',
            onClick: handleActionSelect.bind(null, record),
            ifShow: () => {
              return true;
            },
          },
        ],
        select: (key) => {
          Message.info(`您点击了，${key} 按钮`);
        },
      });
    },
  });

  const [register, {}] = useForm({
    labelColProps: {
      span: 6,
    },
    wrapperColProps: {
      span: 18,
    },
    labelWidth: 80,
    layout: 'inline',
    rowProps: { gutter: [8, 8] },
    colProps: {
      xs: 24,
      sm: 24,
      md: 12,
      lg: 8,
      xl: 6,
    },
    schemas,
  });

  function formAdvanced(status) {
    console.log(status);
    actionRef.value.redoHeight();
  }

  function addTable() {
    showModal.value = true;
  }

  const loadDataTable = async (res) => {
    return await getTableList({ ...formParams, ...params.value, ...res });
  };

  function onCheckedRow(rowKeys) {
    console.log(rowKeys);
  }

  function reloadTable() {
    actionRef.value.reload();
  }

  function handleEdit(record: Recordable) {
    console.log('点击了编辑', record);
    router.push({ name: 'BasicInfo', params: { id: record.id } });
  }

  function handleDelete(record: Recordable) {
    console.log('点击了删除', record);
    Message.info('点击了删除');
  }

  function handleNegative(record: Recordable) {
    console.log('点击了取消', record);
    Message.info('点击了取消');
  }

  function handleSubmit(values: Recordable) {
    console.log(values);
    if (values) {
      reloadTable();
    }
  }

  function handleReset(values: Recordable) {
    console.log(values);
  }

  function handleActionSelect(record: Recordable, key: string) {
    console.log(record);
    console.log(key);
    Message.info(`您点击，${key} 按钮`);
  }
</script>

<style lang="less" scoped></style>
